//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "RiskyBond.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Currency.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CRiskyBond::CRiskyBond (boost::shared_ptr<QuantLib::RiskyBond>& childNative, Object^ owner) : CInstrument(CRiskyBond::typeid)
{
#ifdef HANDLE
	_phRiskyBond = NULL;
#endif
	_ppRiskyBond = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyBond));
}
Cephei::QL::Experimental::Credit::CRiskyBond::CRiskyBond (QuantLib::RiskyBond& childNative, Object^ owner) : CInstrument(CRiskyBond::typeid)
{
#ifdef HANDLE
	_phRiskyBond = NULL;
#endif
	_ppRiskyBond = new boost::shared_ptr<QuantLib::RiskyBond> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyBond));
    _RiskyBondOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Experimental::Credit::CRiskyBond::CRiskyBond (CRiskyBond^ copy) : CInstrument(CRiskyBond::typeid)
{
#ifdef HANDLE
	_phRiskyBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRiskyBond = new boost::shared_ptr<QuantLib::RiskyBond> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyBond));
    }
}
Cephei::QL::Experimental::Credit::CRiskyBond::CRiskyBond (PLATFORM::Type^ t) : CInstrument(CRiskyBond::typeid)
{
#ifdef HANDLE
	_phRiskyBond = NULL;
#endif
	if (!t->IsSubclassOf(CRiskyBond::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CRiskyBond::CRiskyBond (QuantLib::Handle<QuantLib::RiskyBond>& childNative, Object^ owner)  : CInstrument(CRiskyBond::typeid)
{
	_phRiskyBond = &childNative;
	_ppRiskyBond = &static_cast<boost::shared_ptr<QuantLib::RiskyBond>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyBond));
    _RiskyBondOwner = owner;
}
Cephei::QL::Experimental::Credit::CRiskyBond::CRiskyBond (QuantLib::Handle<QuantLib::RiskyBond> childNative)  : CInstrument(CRiskyBond::typeid)
{
	_phRiskyBond = &childNative;
	_ppRiskyBond = &static_cast<boost::shared_ptr<QuantLib::RiskyBond>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CRiskyBond::CRiskyBond (QuantLib::RiskyBond childNative)  : CInstrument(CRiskyBond::typeid)
{
#ifdef HANDLE
	_phRiskyBond = NULL;
#endif
	_ppRiskyBond = new boost::shared_ptr<QuantLib::RiskyBond> (new QuantLib::RiskyBond (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyBond));
}
#endif

Cephei::QL::Experimental::Credit::CRiskyBond::~CRiskyBond ()
{
    if (_ppRiskyBond != NULL)
    {
	    delete _ppRiskyBond;
        _ppRiskyBond = NULL;
    }
}
Cephei::QL::Experimental::Credit::CRiskyBond::!CRiskyBond ()
{
    if (_ppRiskyBond != NULL)
    {
	    delete _ppRiskyBond;
    }
}
QuantLib::RiskyBond& Cephei::QL::Experimental::Credit::CRiskyBond::GetReference ()
{
    if (_ppRiskyBond == NULL) throw REFNEW NativeNullException ();
	return **_ppRiskyBond;
}
boost::shared_ptr<QuantLib::RiskyBond>& Cephei::QL::Experimental::Credit::CRiskyBond::GetShared ()
{
    if (_ppRiskyBond == NULL) throw REFNEW NativeNullException ();
	return *_ppRiskyBond;
}
QuantLib::RiskyBond* Cephei::QL::Experimental::Credit::CRiskyBond::GetPointer ()
{
    if (_ppRiskyBond == NULL) throw REFNEW NativeNullException ();
	return &**_ppRiskyBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RiskyBond>& Cephei::QL::Experimental::Credit::CRiskyBond::GetHandle ()
{
	if (_phRiskyBond == NULL)
	{
		_phRiskyBond = new Handle<QuantLib::RiskyBond> (*_ppRiskyBond);
	}
	return *_phRiskyBond;
}
#endif
bool Cephei::QL::Experimental::Credit::CRiskyBond::HasNative () 
{
	return (_ppRiskyBond != NULL);
}

Double Cephei::QL::Experimental::Credit::CRiskyBond::TotalFutureFlows (DateTime date)
{
    try
    {
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRiskyBond)->totalFutureFlows ( _date );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Experimental::Credit::CRiskyBond::Cashflows::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> > _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >)(*_ppRiskyBond)->cashflows ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = REFNEW CoVector<Cephei::QL::ICashFlow^>(REFNEW CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ICurrency^ Cephei::QL::Experimental::Credit::CRiskyBond::Ccy::get ()
{
    try
    {
    	QuantLib::Currency _rv = (QuantLib::Currency)(*_ppRiskyBond)->ccy ( );   
        Cephei::QL::CCurrency^ _nrv = REFNEW Cephei::QL::CCurrency (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ Cephei::QL::Experimental::Credit::CRiskyBond::DefaultTS::get ()
{
    try
    {
    	Handle<QuantLib::DefaultProbabilityTermStructure> _rv = (Handle<QuantLib::DefaultProbabilityTermStructure>)(*_ppRiskyBond)->defaultTS ( );   
        Cephei::QL::Termstructures::CDefaultProbabilityTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CDefaultProbabilityTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Credit::CRiskyBond::EffectiveDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppRiskyBond)->effectiveDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Experimental::Credit::CRiskyBond::ExpectedCashflows::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> > _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >)(*_ppRiskyBond)->expectedCashflows ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = REFNEW CoVector<Cephei::QL::ICashFlow^>(REFNEW CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Experimental::Credit::CRiskyBond::InterestFlows::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> > _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >)(*_ppRiskyBond)->interestFlows ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = REFNEW CoVector<Cephei::QL::ICashFlow^>(REFNEW CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Credit::CRiskyBond::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppRiskyBond)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Credit::CRiskyBond::MaturityDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppRiskyBond)->maturityDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Experimental::Credit::CRiskyBond::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppRiskyBond)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CRiskyBond::Notional (Microsoft::FSharp::Core::FSharpOption<DateTime>^ date)
{
    try
    {
        QuantLib::Date _date = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (date) ? (QuantLib::Date)ValueHelper::Convert (date->Value) : QuantLib::Date::minDate()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRiskyBond)->notional ( _date );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Experimental::Credit::CRiskyBond::NotionalFlows::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> > _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >)(*_ppRiskyBond)->notionalFlows ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = REFNEW CoVector<Cephei::QL::ICashFlow^>(REFNEW CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CRiskyBond::RecoveryRate::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRiskyBond)->recoveryRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CRiskyBond::RiskfreeNPV::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRiskyBond)->riskfreeNPV ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Experimental::Credit::CRiskyBond::YieldTS::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure> _rv = (Handle<QuantLib::YieldTermStructure>)(*_ppRiskyBond)->yieldTS ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

